package com.example.demo.domain;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.tanx.cqrs.aggregate.Aggregate;
import com.tanx.cqrs.command.handler.CommandHandler;
import com.tanx.cqrs.event.EventBus;
import com.tanx.cqrs.eventsourcing.Id;
import com.tanx.cqrs.eventsourcing.handler.EventSourcingHandler;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.UUID;

/**
 *
 */
@Aggregate
@Data
public class Company {
    @Id
    private String uuid = UUID.randomUUID().toString().replaceAll("-", "");

    @Autowired
    @JsonIgnore
    private transient EventBus eventBus;

    private String name;

    @CommandHandler
    public void setCompanyName(CompanySetName name) {
        System.out.println(111);
        eventBus.sendEvent(new NameSeted(this, name.getNewName()));
    }

    @EventSourcingHandler
    public void source(NameSeted nameSeted) {
        System.out.println("Company source111111111111111111");
        this.name = nameSeted.getName();
    }

}
